//
//  WHOAurhViewController.swift
//  WHSinaWeiBo
//
//  Created by 王寒 on 16/4/9.
//  Copyright © 2016年 wanghan. All rights reserved.
//

import UIKit
import SVProgressHUD

class WHOAurhViewController: UIViewController {

    let userInfoKey = "com.devwhan.weibo"
    
    let webView = UIWebView()
    //自定义根视图为WebView
    override func loadView() {
        view = webView
        webView.delegate = self
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=3845253887&redirect_uri=http://devwhan.com"
        let url = NSURL(string: urlString)
        let request = NSURLRequest(URL: url!)
        webView.loadRequest(request)
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: nil, title: "返回", target: self, action: "pop")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: nil, title: "自动填充", target: self, action: "fullAccount")
        
    }
    

    
    
    func fullAccount(){
        webView.stringByEvaluatingJavaScriptFromString("document.getElementById('userId').value = '347886184@qq.com'")
        
        webView.stringByEvaluatingJavaScriptFromString("document.getElementById('passwd').value = 'yongyuanaiyizu'")
    }
    
    @objc private func pop(){
        dismissViewControllerAnimated(true, completion: nil)
    }
}


//遵守webView协议
extension WHOAurhViewController: UIWebViewDelegate{
    //开始加载页面
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
        
    }
    
    //加载完毕
    func webViewDidFinishLoad(webView: UIWebView) {
        
        let webTitle = webView.stringByEvaluatingJavaScriptFromString("document.title")
        title = webTitle

        SVProgressHUD.dismiss()
    }
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        let urlString = request.URL?.absoluteString ?? ""
        let flag = "code="
        
        if urlString.containsString(flag){
            
            let rang = urlString.rangeOfString(flag)!
            let code = urlString.substringFromIndex(rang.endIndex)
            
            //根据获取到的code 请求令牌
            WHUserAccountViewModel.sharedAccountViewModel.loadAccessToken(code, finished: { (isSuccess) -> () in
                
                if !isSuccess {
                    //登录失败
                    SVProgressHUD.showErrorWithStatus("登录失败")
                    return
                }
                //请求成功
               print("successful---------")
//                SVProgressHUD.showSuccessWithStatus("登录成功")
                //登录成功,跳转到主页面
                self.dismissViewControllerAnimated(false, completion: { () -> Void in
                    NSNotificationCenter.defaultCenter().postNotificationName(WHSwitchRootViewController, object: nil)
                })
            })
            return false
        }

        return true
    }
    
    
    
       
}
